#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>

using namespace std;
void header(int sig)
{
    pid_t rid;
    while((rid = waitpid(-1,nullptr,WNOHANG)) > 0)
    {
        cout << "wait :" << rid << " success" << endl;
    }
}

int main()
{
    srand(time(nullptr));
    signal(SIGCHLD, header);
    //signal(SIGCHLD, SIG_IGN);
    // 创建10个子进程
    for (int i = 10; i > 0; i--)
    {
        pid_t id = fork();
        if (id == 0)
        {
            cout << "I am child:" << getpid() << endl;
            sleep(rand() % 2 + 1);
            cout << "child quit:" << getpid() << endl;
            sleep(rand() % 2 + 1);
            exit(0);
        }
        sleep(rand() % 3 + 3);
    }

    while(1)
    {
        cout << "I am father:" << getpid() << endl;
        sleep(1);
    }
}